package test;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

/**
 * Servlet implementation class Members
 */
public class MembersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final String JSP = "/WEB-INF/jsp/members.jsp";
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Query query = new Query("Member", KeyFactory.createKey("Ancestor",
				"ANCESTOR"));

		DatastoreService ds = Services.datastore;
		List<Entity> results = ds.prepare(query).asList(FetchOptions.Builder.withDefaults());
		List<Member> members = new LinkedList<>();
		for (Entity e : results) {
			members.add(new Member(e.getProperty(Member.FIRSTNAME).toString()));
		}
		request.setAttribute("members", members);

		request.getRequestDispatcher(JSP).forward(request, response);
	}

}
